﻿$(document).ready(function () {
    //Initialization
    SetTextBoxPrimaryPayer();
    SetTextBoxSecondaryPayer();
    SetTextBoxTertieryPayer();
    SetTextBoxIdentityCard();

    ///TextBoxGender
    $("#SalutationId").change(
        function () {
            if ($("#SalutationId").val() == 1) {
                $("#gender").val("Male");
            } else if ($("#SalutationId").val() == 2) {
                $("#gender").val("Female");
            } else {
                $("#gender").val("");
            }
            $("#GenderId").val($("#SalutationId").val());
        }
    );

    ///TextBoxAge
    function getYesterdayDate() {
        var date = new Date;
        date.setDate(date.getDate() - 1);
        return date;
    }
    $("#TextBoxDob").datepicker({
        showOtherMonths: true,
        dateFormat: "dd/mm/yy",
        changeYear: true,
        changeMonth: true,
        maxDate: getYesterdayDate(),
        yearRange: "-70:+0",
        onSelect: function (dateStr) {
            $('#mws-validate').validate().element(this);
            $("#TextBoxAge").val(getAge($("#TextBoxDob").val()));
        }
    });
    //End TextBoxAge

    //Insurance Valid From Valid to
    $(".mws-datepicker-range2020").datepicker({
        showOtherMonths: true,
        dateFormat: "dd/mm/yy",
        changeYear: true,
        changeMonth: true,
        yearRange: "-20:+20",
    });

    //CheckBox IsEmployee
    $("#CheckBoxPrimaryIsEmployee").click(function () {
        if (this.checked) {
            $("#HiddenPrimaryIsEmployee").val("1");
            $("#PrimaryPayerEmployeeId").attr("readonly", false);
        } else {
            $("#HiddenPrimaryIsEmployee").val("0");
            $("#PrimaryPayerEmployeeId").attr("readonly", true);
            $("#PrimaryPayerEmployeeId").val("");
        }

    });
    $("#CheckBoxSecondaryIsEmployee").click(function () {
        if (this.checked) {
            $("#HiddenSecondaryIsEmployee").val("1");
            $("#SecondaryPayerEmployeeId").attr("readonly", false);
        } else {
            $("#HiddenSecondaryIsEmployee").val("0");
            $("#SecondaryPayerEmployeeId").attr("readonly", true);
            $("#SecondaryPayerEmployeeId").val("");
        }

    });
    $("#CheckBoxTertieryIsEmployee").click(function () {
        if (this.checked) {
            $("#HiddenTertieryIsEmployee").val("1");
            $("#TertieryPayerEmployeeId").attr("readonly", false);
        } else {
            $("#HiddenTertieryIsEmployee").val("0");
            $("#TertieryPayerEmployeeId").attr("readonly", true);
            $("#TertieryPayerEmployeeId").val("");
        }

    });

    //DropDown Private Payer

    $("#PrimaryPayerCategoryId").change(
        function(){
            SetTextBoxPrimaryPayer();
        }
     );
    $("#SecondaryPayerCategoryId").change(
        function () {
            SetTextBoxSecondaryPayer();
        }
    );
    $("#TertieryPayerCategoryId").change(
        function(){
            SetTextBoxTertieryPayer();
        }    
    );

    
    function SetTextBoxPrimaryPayer(){
        if ($("#PrimaryPayerCategoryId").val() == "" || $("#PrimaryPayerCategoryId").val() == 4) {
                $("#PrimaryPayerValidFrom").attr("readonly", true);
                $("#PrimaryPayerValidFrom").removeClass("required");
                $("#PrimaryPayerValidTo").attr("readonly", true);
                $("#PrimaryPayerValidTo").removeClass("required");
                $("#PrimaryPayerInsuranceNumber").attr("readonly", true);
                $("#PrimaryPayerFax").attr("readonly", true);

            } else {
                $("#PrimaryPayerValidFrom").attr("readonly", false);
                $("#PrimaryPayerValidFrom").addClass("required");
                $("#PrimaryPayerValidTo").attr("readonly", false);
                $("#PrimaryPayerValidTo").addClass("required");
                $("#PrimaryPayerInsuranceNumber").attr("readonly", false);
                $("#PrimaryPayerFax").attr("readonly", false);
            }
    }
     function SetTextBoxSecondaryPayer(){
        if ($("#SecondaryPayerCategoryId").val() == 4 || $("#SecondaryPayerCategoryId").val() == "") {
                $("#SecondaryPayerValidFrom").attr("readonly", true);
                 $("#SecondaryPayerValidFrom").removeClass("required");
                $("#SecondaryPayerValidTo").attr("readonly", true);
                $("#SecondaryPayerValidTo").removeClass("required");
                $("#SecondaryPayerInsuranceNumber").attr("readonly", true);
                $("#SecondaryPayerFax").attr("readonly", true);
            } else {
                $("#SecondaryPayerValidFrom").attr("readonly", false);
                 $("#SecondaryPayerValidFrom").addClass("required");
                $("#SecondaryPayerValidTo").attr("readonly", false);
                $("#SecondaryPayerValidTo").addClass("required");
                $("#SecondaryPayerInsuranceNumber").attr("readonly", false);
                $("#SecondaryPayerFax").attr("readonly", false);
            }
    }
    function SetTextBoxTertieryPayer(){
            if ($("#TertieryPayerCategoryId").val() == 4 || $("#TertieryPayerCategoryId").val() == "") {
                $("#TertieryPayerValidFrom").attr("readonly", true);
                $("#TertieryPayerValidFrom").removeClass("required");
                $("#TertieryPayerValidTo").attr("readonly", true);
                 $("#TertieryPayerValidTo").removeClass("required");
                $("#TertieryPayerInsuranceNumber").attr("readonly", true);
                $("#TertieryPayerFax").attr("readonly", true);
            } else {
                $("#TertieryPayerValidFrom").attr("readonly", false);
                $("#TertieryPayerValidFrom").addClass("required");
                $("#TertieryPayerValidTo").attr("readonly", false);
                 $("#TertieryPayerValidTo").addClass("required");
                $("#TertieryPayerInsuranceNumber").attr("readonly", false);
                $("#TertieryPayerFax").attr("readonly", false);
            }
    }
    ///GetDateTime clock client side update
    var serverDate;
    function getServerDate() {
        $.get(rooturl + "frontofficeregistration/getclock", function (result) {
            serverDate = new Date(Date.parse(result));
        });
    }
    function tick() {
        if (serverDate) {
            serverDate.setSeconds(serverDate.getSeconds() + 1);
            var min = serverDate.getMinutes();
            if (min < 10) min = "0" + min;
            var sec = serverDate.getSeconds();
            if (sec < 10) sec = "0" + sec;
            var month = serverDate.getMonth() + 1;
            var clock = serverDate.getDate() + "-" + month + "-" + serverDate.getFullYear() + "  " + serverDate.getHours() + ":" + min + ":" + sec;
            $('#reg_time').val(clock);
        }
    }
    $(function () {
        if (window.location == rooturl+"frontoffice/registration/register") {
            getServerDate();
            setInterval(tick, 1000);
        }

    });


    ///OnCheck CheckBox Same as Present Address
    $("#CheckboxPermanentAddress").click(function () {
        if (this.checked) {
            $("#TextBoxAddressDescPermanent").val($("#TextBoxAddressDescPresent").val());
            $("#TextBoxAreaPermanent").val($("#TextBoxAreaPresent").val());
            $("#TextBoxCityPermanent").val($("#TextBoxCityPresent").val());
            $("#TextBoxStatePermanent").val($("#TextBoxStatePresent").val());
            $("#TextBoxCountryPermanent").val($("#TextBoxCountryPresent").val());
            $("#TextBoxZipCodePermanent").val($("#TextBoxZipCodePresent").val());
            $("#TextBoxPhoneNumberPermanent").val($("#TextBoxPhoneNumberPresent").val());

            $("#HiddenPermanentAddressAreaId").val($("#HiddenPresentAddressAreaId").val());
            $("#HiddenPermanentAddressCityId").val($("#HiddenPresentAddressCityId").val());
            $("#HiddenPermanentAddressCountryId").val($("#HiddenPresentAddressCountryId").val());
            $("#HiddenPermanentProvinceId").val($("#HiddenPresentProvinceId").val());
        } else {
            $("#TextBoxAddressDescPermanent").val("");
            $("#TextBoxAreaPermanent").val("");
            $("#TextBoxCityPermanent").val("");
            $("#TextBoxStatePermanent").val("");
            $("#TextBoxCountryPermanent").val("");
            $("#TextBoxZipCodePermanent").val("");
            $("#TextBoxPhoneNumberPermanent").val("");

            $("#HiddenPermanentAddressAreaId").val("");
            $("#HiddenPermanentAddressCityId").val("");
            $("#HiddenPermanentAddressCountryId").val("");
            $("#HiddenPermanentProvinceId").val("");
        }

    });



    //Identity Card
    //Flag
    $("#IdentityCardTypeId").change(
        function () {
            SetTextBoxIdentityCard();
        }
    );

    function SetTextBoxIdentityCard(){
        if ($("#IdentityCardTypeId").val() == 3) {
                $("#divIdentityCardbNumber").removeClass("mws-form-col-5-8");
                $("#divButtonPassportDetail").css({ display: "block" });
                $("#divIdentityCardbNumber").addClass("mws-form-col-3-8");
            } else {
                $("#divIdentityCardbNumber").removeClass("mws-form-col-3-8");
                $("#divIdentityCardbNumber").addClass("mws-form-col-5-8");
                $("#divButtonPassportDetail").css({ display: "none" });
            }
    }
    function OpenPopupConfirm() {
        $("#dialog:ui-dialog").dialog("destroy");

        $("#dialog-confirm").dialog({
            resizable: false,
            height: 160,
            modal: true,
            buttons: {
                "Yes": function () {
                    window.location = rooturl+"frontoffice/registration/registrationnextstep";
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
    }


    //Submit Form Ajax
    $("#mws-validate").submit(
        function () {
            if ($("#mws-validate").valid() == true) {
//                                $.blockUI({
//                                    message: $("#ajaxLoader")
//                                });
                $.blockUI();
                var post_data = $(this).serialize();
                var form_action = $(this).attr("action");
                var form_method = $(this).attr("method");
                $.ajax({
                    type: form_method,
                    url: form_action,
                    cache: false,
                    dataType: "json",
                    data: post_data,
                    success: function (data) {
                        window.scrollTo(0, 0);
                        setTimeout(function SuccessHandler() {
                            $.unblockUI();
                            $("#notif").html(data['strNotification']); 
                            $("#MedicalRecordNumber").val(data['mrn']);
                            $("#PatientId").val(data['patientId']);
                            //$("#buttonAction").attr("disabled", "disabled");
                            if (data['strNotification'] != null)
                                OpenPopupConfirm();

                        }, 2000);

                    },
                    error: function () {
                        alert("Error Occured on Post Ajax");
                        $.unblockUI();
                    },
                    complete: function () {

                    }
                });
            }
            return false;
        });


});
//End Document.ready

///FileUpload
$(function () {
    //Function to upload file.
    new AjaxUpload('#uploadFile', {
        action: rooturl+'frontoffice/registration/uploadhandler',
        name: 'upload',
        onComplete: function (file) {
            //$('<div><img src="../../../Uploads/' + file + '" /> </div>').appendTo('#fileList');
            $('#fileList').html('<div><img src="' + rooturl + 'Images/PatientImages/' + file + '" style="height:175px;width:150px;"/> </div>');
            $('#uploadStatus').html('');
        },
        onSubmit: function (file, ext) {
            if (!(ext && /^(gif|jpeg|png|jpg)$/i.test(ext))) {
                alert('Invalid File Format.');
                return false;
            }
            $('#uploadStatus').html('<img src="' + rooturl + 'Images/Icons/ajax-loader.gif" style="height:30px;width:100px;" />');
        }
    });
});


//Popup Area
function OpenPopUpPresentArea(url) {
    $("#HiddenIsPermanentAddress").val("0");
    $("#mws-form-dialog").dialog("open");
    $("#modalIframeForm").attr("src", rooturl + "home/popup/popupsearcharea");
    return false;
}
function OpenPopUpPermanentArea(url) {
    $("#HiddenIsPermanentAddress").val("1");
    $("#mws-form-dialog").dialog("open");
    $("#modalIframeForm").attr("src", rooturl + "home/popup/popupsearcharea");
    return false;
}
function OpenPopUpRegNextStep() {
    $("#mws-form-dialog").dialog("open");
    $("#modalIframeForm").attr("src", rooturl + "home/popup/popupregistrationnextstep");
    return false;
}

function IsPermanentAddress() {
    return $("#HiddenIsPermanentAddress").val();
}

function setPresentAreaTextBox(AreaId, AreaName, CityId, CityName, CountryId, CountryName, ProvinceId, ProvinceName) {
    $("#TextBoxAreaPresent").val(AreaName);
    $("#HiddenPresentAddressAreaId").val(AreaId);
    $("#HiddenPresentAddressCityId").val(CityId);
    $("#TextBoxCityPresent").val(CityName);
    $("#TextBoxStatePresent").val(ProvinceName);
    $("#HiddenPresentAddressCountryId").val(CountryId);
    $("#TextBoxCountryPresent").val(CountryName);
    $("#HiddenPresentProvinceId").val(ProvinceId);
    $('#modalIframeForm').attr('src', '');
    $("#mws-form-dialog").dialog("close");

}
function setPermanentAreaTextBox(AreaId, AreaName, CityId, CityName, CountryId, CountryName, ProvinceId, ProvinceName) {
    $("#TextBoxAreaPermanent").val(AreaName);
    $("#HiddenPermanentAddressAreaId").val(AreaId);
    $("#HiddenPermanentAddressCityId").val(CityId);
    $("#TextBoxCityPermanent").val(CityName);
    $("#TextBoxStatePermanent").val(ProvinceName);
    $("#HiddenPermanentAddressCountryId").val(CountryId);
    $("#TextBoxCountryPermanent").val(CountryName);
    $("#HiddenPermanentProvinceId").val(ProvinceId);
    $('#modalIframeForm').attr('src', '');
    $("#mws-form-dialog").dialog("close");

}



function IsDoctorHandler() {
    return $("#HiddenIsDoctorHandler").val();
}


function OpenPopUpPassportDetail() {
    $("#mws-form-dialog").dialog("open");
    $("#modalIframeForm").attr("src", rooturl + "home/popup/popuppassportdetail");
    return false;
}

function SetTextBoxPassportId(passportId){
    $("#IdentityCardbNumber").val(passportId);
    CloseDialog();
}
function CloseDialog(){
    $('#modalIframeForm').attr('src', '');
    $("#mws-form-dialog").dialog("close");
}
